import Vue from 'vue'
import Vuex from 'vuex'
import { setStorage, getStorage } from '@/utils/storage'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 当前登录用户的登录状态
    user: getStorage('user'),
    cachePages: ['LayoutIndex']
  },
  mutations: {
    // 登录成功后，把 token 存储在这里（data）
    setUser (state, data) {
      state.user = data
      // 为防止页面刷新，导致数据丢失，还需要将数据存储在本地
      setStorage('user', state.user)
    },
    addCachePages (state, pageNmae) {
      if (!state.cachePages.includes(pageNmae)) {
        state.cachePages.push(pageNmae)
      }
    },
    removeCachePages (state, pageNmae) {
      const index = state.cachePages.indexOf(pageNmae)
      if (index !== -1) {
        state.cachePages.splice(index, 1)
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
